<?php 

if (($action == "edit") && ($go == "make-changes")) { // will add another instance of the theme (a new version) to the DB 

	if ($_POST['theme_text'] == "") { 
		// if the theme content is blank...
		// insert new record into the db, get the new id of the record, redirect to alert user
		
		$insertSQL = sprintf("INSERT INTO themes (cid, mid, vid, theme_num, theme_title, theme_text) VALUES (%s, %s, %s, %s, %s, %s)", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['theme_num'], "text"),
			GetSQLValueString($_POST['theme_title'], "text"),
			GetSQLValueString($_POST['theme_text'], "text")
		);		
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
		
		$query_id_check = sprintf("SELECT id FROM %s WHERE cid='%s' AND mid='%s' AND vid='%s'", $dbTable, $cid, $mid, $_POST['vid']);
		$id_check = mysql_query($query_id_check, $connection) or die(mysql_error());
		$row_id_check = mysql_fetch_assoc($id_check);
		
		header("location:../index.php?section=module_theme&action=".$action."&go=missing&view=course_detail&dbTable=themes&cid=".$cid."&mid=".$mid."&id=".$row_id_check['id']."&modal_window=".$modal_window."&msg=2");
	}
	
	else {
		
		$insertSQL = sprintf("INSERT INTO themes (cid, mid, vid, theme_num, theme_title, theme_text) VALUES (%s, %s, %s, %s, %s, %s)", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['theme_num'], "text"),
			GetSQLValueString($_POST['theme_title'], "text"),
			GetSQLValueString($_POST['theme_text'], "text")
		);	
		//echo $insertSQL;
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
		
		$query_id_check = sprintf("SELECT id FROM %s WHERE cid='%s' AND mid='%s' AND vid='%s'", $dbTable, $cid, $mid, $_POST['vid']);
		$id_check = mysql_query($query_id_check, $connection) or die(mysql_error());
		$row_id_check = mysql_fetch_assoc($id_check);
		//echo $query_id_check."<br>";
		$insertGoTo = "../index.php?section=module_theme&action=".$action."&go=confirm&view=course_detail&dbTable=themes&cid=".$cid."&mid=".$mid."&id=".$row_id_check['id']."&modal_window=".$modal_window."&msg=1";
		//echo $insertGoTo;
		header(sprintf("Location: %s", $insertGoTo));
	}
	
}

if ((($action == "add") || ($action == "edit") || ($action == "review")) && ($go == "draft")) {
	
	if ($_POST['theme_text'] == "") {
		header("location:../index.php?section=module_theme&action=".$action."&go=missing&view=course_detail&dbTable=themes&cid=".$cid."&mid=".$mid."&id=".$id."&modal_window=".$modal_window."&msg=2");
	}
	
	else {
	
	$updateSQL = sprintf("UPDATE themes
		SET 
		cid=%s,
		mid=%s,
		vid=%s,
		theme_num=%s, 
		theme_title=%s,
		theme_text=%s
		WHERE id=%s", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['theme_num'], "int"),
			GetSQLValueString($_POST['theme_title'], "text"),
			GetSQLValueString($_POST['theme_text'], "text"),
			GetSQLValueString($id, "int")
		);
					
	mysql_select_db($database, $connection);
	$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
	//echo $updateSQL;
	if (($go == "draft") || ($go == "missing")) $updateGoTo = "../index.php?section=module_theme&action=".$action."&go=confirm&view=course_detail&dbTable=themes&cid=".$cid."&mid=".$mid."&id=".$id."&modal_window=".$modal_window."&msg=1";
	else $updateGoTo = $updateGoTo."&msg=4";
	header(sprintf("Location: %s", $updateGoTo));
	}
}


?>